package com.example.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.example.util.Result;
import com.example.entity.Store;
import com.example.service.StoreService;

@RestController
@RequestMapping("/store")
public class StoreController {

    @Autowired
    private StoreService storeService;

    @PostMapping("/create")
    public Result<?> createStore(@RequestBody Store store){
        return storeService.createStore(store) ? Result.success("创建成功"):Result.fail("创建失败");
    }

    @GetMapping("/{sId}")
    public Result<Store> getStoreById(@PathVariable Long sId){
        Store store = storeService.getStoreById(sId);
        return store !=null ? Result.success(store):Result.fail("未找到该店铺");
        
    }

    @GetMapping("merchant/{mId}")
    public Result<Store> getStoreByMerchantId(@PathVariable Long mId){
            Store store = storeService.getStoreByMerchantId(mId);
            return store != null ? Result.success(store) : Result.fail("该商家未创建店铺");
       }
    @PutMapping("/update")
    public Result<?> updateStore(@RequestBody Store store){
        return storeService.updateStore(store)?Result.success("更新成功"):Result.fail("更新失败");
    }

    @PutMapping("/status")
    public Result<?> changeStatus(@RequestParam Long sId, @RequestParam String status){
        return storeService.changeStoreStatus(sId, status)?Result.success("状态更新成功"):Result.fail("状态更新失败");
    }
   @GetMapping("/list")
    public Result<List<Store>> listActiveStores() {
        return Result.success(storeService.listActiveStores());
   }
}
